package com.hui.web.servlet.studentServlet;

import com.hui.bean.ClassBean;
import com.hui.bean.RoomBean;
import com.hui.bean.StudentBean;
import com.hui.service.IClassService;
import com.hui.service.IRoomService;
import com.hui.service.IStudentService;
import com.hui.service.impl.ClassServiceImpl;
import com.hui.service.impl.RoomServiceImpl;
import com.hui.service.impl.StudentServiceImpl;
import com.hui.util.DateUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "StudentAddExecuteServlet",urlPatterns = "/student/add/execute")
@MultipartConfig
public class StudentAddExecuteServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String userName = request.getParameter("userName");

        Part photo = request.getPart("photo");



        String sex = request.getParameter("sex");

        String className = request.getParameter("allClass");

        String roomAddress = request.getParameter("allRoom");

        String phone = request.getParameter("phone");

        String inTime = request.getParameter("inTime");

        String refresh = request.getParameter("refresh");

        System.out.println(sex);
        if (null!=refresh){
            IClassService classService=new ClassServiceImpl();
            List<ClassBean> allClasses = classService.getAllClasses();
            request.setAttribute("allClasses",allClasses);
            IRoomService roomService=new RoomServiceImpl();
            List<RoomBean> roomBeans = roomService.getCanInRoomsByRoomType(sex.equals("1")?"M":"F");
            request.setAttribute("canInRooms",roomBeans);
            request.setAttribute("username",userName);
            request.setAttribute("sex",sex);
            request.setAttribute("classname",className);
            request.setAttribute("phone",phone);
            request.setAttribute("inTime",inTime);
            request.getRequestDispatcher("/page/student/studentin.jsp").forward(request, response);
            return;
        }
        String headInfo = photo.getHeader("Content-Disposition");

        String fileName = parseName(headInfo);
        String realPath = request.getServletContext().getRealPath(File.separator + "image");
        String fullFilePath = realPath + File.separator + fileName;
        photo.write(fullFilePath);


//        System.out.println(userName+","+sex+","+className+","+roomAddress+","+phone+","+inTime);
        IClassService classService=new ClassServiceImpl();
        ClassBean classBean = classService.getClassByClassName(className);
        IRoomService roomService=new RoomServiceImpl();
        RoomBean roomBean = roomService.getRoomByRoomAddress(roomAddress);

        StudentBean studentBean = new StudentBean(0,userName,sex,fileName,phone, DateUtils.strToDate(inTime),classBean,roomBean);
        IStudentService studentService=new StudentServiceImpl();
        studentService.addStudentInRoom(studentBean);

        response.sendRedirect("/student/index");

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    private String parseName(String headerInfo) {
        String[] strings = headerInfo.split("\"");
        return strings[strings.length - 1];
    }


}
